package com.tongzhan.service.impl;

import com.tongzhan.data.vo.DutiesVO;
import com.tongzhan.entity.religion.Duties;
import com.tongzhan.repository.religion.DutiesRepository;
import com.tongzhan.service.DutiesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DutiesServiceImpl implements DutiesService {

    @Autowired
    private DutiesRepository dutiesRepository;

    @Override
    public List<DutiesVO> getDutiesList() {
        //查询宗教下的权限列表
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.ASC, "Sort"));
        query.addCriteria(Criteria.where("Type").is(4));
        List<Duties> dutiesList = dutiesRepository.findAll(query);
        //封装返回对象列表
        List<DutiesVO> dutiesVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(dutiesList)) {
            for (Duties duties : dutiesList) {
                DutiesVO dutiesVO = new DutiesVO();
                dutiesVO.setDutiesCode(duties.getDutiesCode());
                dutiesVO.setDutiesName(duties.getDutiesName());
                dutiesVOS.add(dutiesVO);
            }
        }
        return dutiesVOS;
    }
}
